package com.appkudos.view;

import com.appkudos.controller.UserController;
import com.appkudos.model.Group;
import com.appkudos.model.Recommendation;
import com.appkudos.model.User;
import com.appkudos.view.R;

import android.app.Activity;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.Toast;

public class AppKudos extends TabActivity {
    /** Called when the activity is first created. */
	User user;
	User otherUser;
	Group group;
	UserController uc;
	boolean isRec = false;
	String tab = null;
	String display_option;
	
	com.appkudos.model.App app;
	String APPname;
	String APPdesc;
	String APPid;
	String tab_prefix = "";
	
	String recid;
	String sender;
	String receiver;
	String msg;
	String isGroup;
	String isSent;
	String appid;
	String menu = null;
	
	
	@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent launchIntent = getIntent();
        Bundle extras = launchIntent.getExtras();
        uc = new UserController();
        user = uc.getCurrentUser(extras);
        otherUser = uc.getFriend(extras);
        tab = extras.getString(this.getString(R.string.tab));
        display_option = extras.getString(this.getString(R.string.display_opt));
        app = uc.getApp(extras);
        
        recid = extras.getString("recid");
    	sender = extras.getString("sender");
    	receiver = extras.getString("receiver");
    	msg = extras.getString("msg");
    	isGroup = extras.getString("isGroup");
    	isSent = extras.getString("isSent");
    	appid =  extras.getString("appid");
    	menu = extras.getString("menu");
    	
    	
	    // get group info if exists
	    group = uc.getCurrentGroup(extras);

	    if(otherUser != null && otherUser.getUsername().compareTo(user.getUsername())==0){
	    	otherUser = null;
	    }
	    
        final TabHost tabHost = getTabHost();
        if (menu == null){
		    if(group != null){
		    	
		    	if(display_option != null){
		    		if(display_option.equals("select_rec")){
		    			initUser(tabHost);
		    		}else{
		    			initGroup(tabHost);
		    		}
		    	}else{
		    		initGroup(tabHost);
		    	}
		    }
		    else{
		    	initUser(tabHost);
		    }
        }else{
        	otherUser = null;
        	initUser(tabHost);
        }
        
    }
    
    private void initUser(TabHost tabHost){
        // Reusable intent for each tab
    	Intent intent;
    	
    	
    	if(otherUser != null){
    		tab_prefix = "";
    	}else{
    		tab_prefix = "My ";
    	}
        // user profile tab
        //intent = new Intent().setClass(this, SelectAppActivity.class);
    	//intent = new Intent().setClass(this, JoinGroupActivity.class);
    	intent = new Intent().setClass(this, UserProfileActivity.class);
    	intent = uc.passCurrentUser(intent, user);
    	 if(otherUser != null){
     		intent = uc.passFriend(intent, otherUser);
         }
         if((tab != null && tab.equals(this.getString(R.string.tab_user_profile))) && display_option != null){
         	intent.putExtra(this.getString(R.string.display_opt), display_option);
         	if (display_option.equals("view_rec")){
         		
//        		intent = uc.passRec(intent, rec);
                intent.putExtra("recid", recid);
                intent.putExtra("sender", sender);
                intent.putExtra("receiver",receiver);
                intent.putExtra("msg", msg);
                intent.putExtra("isGroup",isGroup);
                intent.putExtra("isSent", isSent);
                intent.putExtra("appid", appid);
        	}
         }
         tabHost.addTab(tabHost.newTabSpec("profile")
                .setIndicator(tab_prefix+"Profile", getResources().getDrawable(R.drawable.ic_profile))
                .setContent(intent));
        
        // user apps tab
//       intent = new Intent().setClass(this, WriteReviewActivity.class);
        //intent = new Intent().setClass(this, SelectAppActivity.class);
        intent = new Intent().setClass(this, UserAppsActivity.class);
        intent = uc.passCurrentUser(intent, user);
        if(otherUser != null){
    		intent = uc.passFriend(intent, otherUser);
        }
        if((tab != null && tab.equals(this.getString(R.string.tab_user_app))) && display_option != null){
        	intent.putExtra(this.getString(R.string.display_opt), display_option);
        	if (display_option.equals("select_app") || display_option.equals("view_app") ||display_option.equals("create_rec") ){
        		intent = uc.passApp(intent, app);
        		if(group != null){
        			intent = uc.passCurrentGroup(intent, group);
        		}
        	}
        }	
        tabHost.addTab(tabHost.newTabSpec("apps")
               .setIndicator(tab_prefix+"Apps", getResources().getDrawable(R.drawable.ic_apps))
               .setContent(intent));
        
        // user connections tab
//        intent = new Intent().setClass(this, AddFriendActivity.class);
        intent = new Intent().setClass(this, UserConnectionsActivity.class);
//      intent = new Intent().setClass(this, WriteReviewActivity.class);
        intent = uc.passCurrentUser(intent, user);
        if(otherUser != null){
    		intent = uc.passFriend(intent, otherUser);
        }
        if((tab != null && tab.equals(this.getString(R.string.tab_user_connection))) && display_option != null){
        	intent.putExtra(this.getString(R.string.display_opt), display_option);        	
        }
        tabHost.addTab(tabHost.newTabSpec("connections")
                .setIndicator(tab_prefix+"Connections", getResources().getDrawable(R.drawable.ic_connections))
                .setContent(intent));
        
        if (menu == null){
	        if(tab != null && tab.equals(this.getString(R.string.tab_user_profile))){
	        	tabHost.setCurrentTab(0);
	        }else if(tab != null && tab.equals(this.getString(R.string.tab_user_app))){
	        	tabHost.setCurrentTab(1);
	        }else if(tab != null && tab.equals(this.getString(R.string.tab_user_connection))){
	        	tabHost.setCurrentTab(2);
	        }else{
	        	tabHost.setCurrentTab(0);
	        }
        }else{
        	if (menu.equals("myapps")){
        		tabHost.setCurrentTab(1);
        	}else{
        		if (menu.equals("myconnections")){
            		tabHost.setCurrentTab(2);
            	}else{
            		tabHost.setCurrentTab(0);
            	}
        	}
        }
    }
    
    private void initGroup(TabHost tabHost){
        // Reusable intent for each tab
    	Intent intent;
    	
        // group profile tab
    	//intent = new Intent().setClass(this, LoginActivity.class);
        intent = new Intent().setClass(this, GroupProfileActivity.class);
        intent = uc.passCurrentUser(intent, user);
        intent = uc.passCurrentGroup(intent, group);
   	 if(otherUser != null){
  		intent = uc.passFriend(intent, otherUser);
      }
      if((tab != null && tab.equals(this.getString(R.string.tab_group_profile))) && display_option != null){
      	intent.putExtra(this.getString(R.string.display_opt), display_option);
     	if (display_option.equals("view_rec")){
     		
            intent.putExtra("recid", recid);
            intent.putExtra("sender", sender);
            intent.putExtra("receiver",receiver);
            intent.putExtra("msg", msg);
            intent.putExtra("isGroup",isGroup);
            intent.putExtra("isSent", isSent);
            intent.putExtra("appid", appid);
    	}
      }
        tabHost.addTab(tabHost.newTabSpec("profile")
                .setIndicator("Group Profile", getResources().getDrawable(R.drawable.ic_profile))
                .setContent(intent));

        // group apps tab
        //intent = new Intent().setClass(this, JoinGroupActivity.class);
        intent = new Intent().setClass(this, GroupAppsActivity.class);
        intent = uc.passCurrentUser(intent, user);
        intent = uc.passCurrentGroup(intent, group);
   	 if(otherUser != null){
   		intent = uc.passFriend(intent, otherUser);
       }
       if((tab != null && tab.equals(this.getString(R.string.tab_group_app))) && display_option != null){
       	intent.putExtra(this.getString(R.string.display_opt), display_option);
       	
       }
        tabHost.addTab(tabHost.newTabSpec("apps")
                .setIndicator("Group Apps", getResources().getDrawable(R.drawable.ic_apps))
                .setContent(intent));
        
        // group member tab
        //intent = new Intent().setClass(this, AddFriendActivity.class);
        intent = new Intent().setClass(this, GroupMemberActivity.class);
        intent = uc.passCurrentUser(intent, user);
        intent = uc.passCurrentGroup(intent, group);
   	 if(otherUser != null){
   		intent = uc.passFriend(intent, otherUser);
       }
       if((tab != null && tab.equals(this.getString(R.string.tab_group_connection))) && display_option != null){
       	intent.putExtra(this.getString(R.string.display_opt), display_option);
       	
       }
        tabHost.addTab(tabHost.newTabSpec("Members")
                .setIndicator("Group Members", getResources().getDrawable(R.drawable.ic_connections))
                .setContent(intent));
        
        
        if(tab != null && tab.equals(this.getString(R.string.tab_group_profile))){
        	tabHost.setCurrentTab(0);
        }else if(tab != null && tab.equals(this.getString(R.string.tab_group_app))){
        	tabHost.setCurrentTab(1);
        }else if(tab != null && tab.equals(this.getString(R.string.tab_group_connection))){
        	tabHost.setCurrentTab(2);
        }else{
        	tabHost.setCurrentTab(0);
        }
    	
    }

    /** {@inheritDoc} */
    /*public View createTabContent(String tag) {
        final TextView tv = new TextView(this);
        tv.setText("Content for tab with tag " + tag);
        return tv;
    }*/
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.layout.menu, menu);
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    	Intent intent = new Intent(this, AppKudos.class);
	    intent = uc.passCurrentUser(intent, user);
  		switch (item.getItemId()) {
            case R.id.text:  
            	intent.putExtra("menu","myprofile");
            	//Toast.makeText(this, "You pressed the text!", Toast.LENGTH_LONG).show();
            	break;
            case R.id.text_app:  
            	intent.putExtra("menu","myapps");
            	//Toast.makeText(this, "You pressed the text!", Toast.LENGTH_LONG).show();
            	break;
            case R.id.text_con:  
            	intent.putExtra("menu","myconnections");
            	//Toast.makeText(this, "You pressed the text!", Toast.LENGTH_LONG).show();
            	break;
        }
  		startActivity(intent);
        return true;
    }
    
}